#include "clientapplication.h"

ClientApplication* ClientApplication::client = NULL;

ClientApplication::ClientApplication(Session* context, QString address, QString port)
{
    this->context = context;
    this->address = address;
    this->port = port;
    this->started = false;
    this->connected = false;
}

void ClientApplication::Init()
{
    socket = new QTcpSocket();
    executor = new MainExeqtor(context);
    transport = new SocketTransport(socket, executor);
    connect(ClientApplication::Instance()->Executor()->getCurrent("authorization"),SIGNAL(proceed()),this, SLOT(ProceedAuthorization()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("authorization"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("register"),SIGNAL(proceed()),this, SLOT(ProceedRegistration()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("register"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("roomsList"),SIGNAL(proceed()),this, SLOT(ProceedRoomsList()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("roomsList"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("joinRoom"),SIGNAL(proceed()),this, SLOT(ProceedJoinRoom()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("joinRoom"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("getUsers"),SIGNAL(proceed()),this, SLOT(ProceedGetUsers()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("getUsers"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("sendMessage"),SIGNAL(proceed()),this, SLOT(ProceedSendMessage()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("sendMessage"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("getMessages"),SIGNAL(proceed()),this, SLOT(ProceedGetMessages()),Qt::QueuedConnection);
    connect(ClientApplication::Instance()->Executor()->getCurrent("getMessages"),SIGNAL(fail(QString)),this, SLOT(Failed(QString)),Qt::QueuedConnection);
    receiveThread = new RecieveThread(transport);
    receiveThread->setObjectName("recieve");
    return;
}

void ClientApplication::StartClient()
{
    Init();
    if (socket->state() == socket->ConnectedState )
        socket->close();
    socket->connectToHost(address, port.toInt());
    receiveThread->start();
    sync = false;
    started = QBool(true);
    connected = QBool(true);
    return;
}

void ClientApplication::StopClient()
{
    receiveThread->terminate();
    if (socket->state() == socket->ConnectedState )
        socket->close();
    started=QBool(false);
    connected = QBool(socket->state() == socket->ConnectedState);
}

void ClientApplication::ProceedAuthorization()
{
    emit authorization();
}

void ClientApplication::ProceedRegistration()
{
    emit registration();
}

void ClientApplication::ProceedRoomsList()
{
    emit roomsList();
}

void ClientApplication::Failed(QString cause)
{
    emit fail(cause);
}

void ClientApplication::ProceedJoinRoom()
{
    emit joinRoom();
}

void ClientApplication::ProceedGetUsers()
{
    emit getUsers();
}

void ClientApplication::ProceedSendMessage()
{
    emit sendMessage();
}

void ClientApplication::ProceedGetMessages()
{
    emit getMessages();
}
